package top.frsf.autocodegen.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.frsf.autocodegen.controller.dto.AutoTableExportDTO;
import top.frsf.autocodegen.entity.AutoTable;
import top.frsf.autocodegen.service.IAutoTableService;
import top.frsf.autocodegen.utils.Result;

import java.sql.SQLException;
import java.util.List;

/**
 * @author FRSF
 * @since 2024/7/30 10:32
 */

@Tag(name = "2 - 数据表管理")
@RestController
@RequestMapping("/autoTable")
public class AutoTableController {

    @Resource
    private IAutoTableService autoTableService;

    @Operation(summary = "数据表列表")
    @GetMapping
    public Result<List<AutoTable>> list() {
        return Result.success(autoTableService.list());
    }


    @Operation(summary = "数据库导出数据表")
    @PostMapping("/export")
    public Result<Boolean> export(@Validated @RequestBody AutoTableExportDTO dto) throws SQLException, ClassNotFoundException {
        autoTableService.export(dto);
        return Result.success(true);
    }

    @Operation(summary = "删除指定ID的生成数据表及其字段表数据")
    @DeleteMapping
    public Result<Boolean> delete(@RequestParam Integer id) {
        autoTableService.deleteTableAndField(id);
        return Result.success(true);
    }

}
